package com.wx.frame.controller;

import com.wx.frame.model.Staff;
import com.wx.frame.model.User;
import com.wx.frame.service.StaffService;
import com.wx.utils.VerifyCode;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by myd on 16/4/18.
 */

@Controller("mainController")
@RequestMapping("/main")
public class MainController {
    /**
     * 注入service
     */
    @Resource(name="staffServiceImpl")
    public StaffService<Staff> staffService;
    VerifyCode verifyCode;

    /**
     * 生成验证码
     * @param req
     * @param resp
     */
    @RequestMapping(value = "/code",method ={RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public void code(HttpServletRequest req, HttpServletResponse resp)
    {
        int result=0;
        try {
            verifyCode = new VerifyCode();
            verifyCode.getCode(req, resp);
        }catch (Exception e)
        {
            e.getStackTrace();
        }
    }


    /**
     * 页面跳转
     * @param cc
     * @param request
     * @return
     */
    @RequestMapping(value = "/index")
    public String index(String cc, HttpServletRequest request,HttpServletResponse response) {
        //框架主界面
        String url = "/frame/frame/index.jsp";
        //取得登录用户信息
         Staff loginUser = (Staff) request.getSession().getAttribute("loginUser");
        //判断session，如果不存在，则跳转到登录界面
        System.out.println("-----------清空session------");
        if(null == loginUser || "".equals(loginUser)) {
            //url = "../../frame/login/login.jsp";
            //url = "/main/logout.action";
            url="redirect:/";
        }

        return url;
    }



    /**
     * 登录验证
     * @param staff
     * @param code_value
     * @param session
     * @return
     */
    @RequestMapping(value = "/login",method ={RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public String login(Staff staff, @RequestParam("code_value") String code_value, HttpSession session)
    {
        String msg="0";
        if (session.getAttribute("code").toString().equals(code_value.toUpperCase()))//全部都转化为大写,然后再比较
        {
            List<Staff> list= staffService.getAll(staff);
            if (list!=null && list.size()>0)
            {
                for (Staff st:list) {
                    session.setAttribute("loginUser",st);
                }
                msg = "1";
            }
            else
            {
                msg = "2";
            }
        }

        return msg;
        //0 验证码错误,1登录成功,2用户名或密码错误
    }

    /**
     * 获取session
     * @return
     */
    @RequestMapping("/session")
    @ResponseBody
    public Map<String,Object> session(HttpSession session){
        //获取session
        Staff staff =(Staff)session.getAttribute("loginUser");
        User loginUser=new User();
        loginUser.setUser_number(staff.getStaff_loginname());
        loginUser.setUser_name(staff.getStaff_name());
        loginUser.setFk_roles_pk(staff.getFk_roles_pk());
        loginUser.setUser_pk(staff.getStaff_pk());
        loginUser.setRoles_name(staff.getStaff_name());
        Map<String,Object> map =new HashMap<>();
        map.put("loginUser",loginUser);
        return map;
    }

    /**
     * 退出登录
     * @param request
     * @return
     */
    @RequestMapping("/logout")
    @ResponseBody
    public String logout(HttpServletRequest request)
    {
        request.getSession().removeAttribute("loginUser");
        String url = "/frame/login/login.jsp";
        return url;
    }
}
